package tv.starfu.components.music.math;

/**
 * This class provides methods related to music theory. 
 *
 * @author Nia Radical
 */
import tv.starfu.components.music.events.Note;
import java.util.HashMap;

public final class MusicTheory {
    
    private String[] PITCH = {"Ab", "A", "Bb", "B", "C", "Db", "D", "Eb", "E", "F", "Gb", "G"};
    private int NUM_OCTAVES = 8;
    private float NOTES_IN_OCTAVE = 12f;
    private int NUM_TONES = (int) (NUM_OCTAVES * NOTES_IN_OCTAVE);
    private Note[] NOTES = new Note[NUM_TONES];
    
    private float A4 = 440.0f;
    
    // USE TREEMAP
    public HashMap NumToFreq;
    public HashMap NumToName;
    public HashMap FreqToNum;
    public HashMap FreqToName;
    public HashMap NameToNum;
    public HashMap NameToFreq;

    public MusicTheory() {
        NumToFreq   = new HashMap();
        NumToName   = new HashMap();
        FreqToNum   = new HashMap();
        FreqToName  = new HashMap();
        NameToNum   = new HashMap();
        NameToFreq  = new HashMap(); 
        
        float A0 = (float) ((A4 * Math.pow(2.0f, 1 / NOTES_IN_OCTAVE)) / 16);
        fillNotes();
        createMaps();
            
        System.out.println(NameToFreq.toString());
        System.out.println(NumToName.toString());
        
    }
    
    private void fillNotes() {
        String[] pitch_octave = new String[NUM_TONES];

        for (int i = 0; i < NUM_TONES; ++i) {
            int num = i + 1;
            int tone = num % (int) NOTES_IN_OCTAVE;
            int oct = (int) (num / NOTES_IN_OCTAVE);
            String noteName = PITCH[tone] + oct;
            pitch_octave[i] = noteName;

            float tempFreq = (float) ((A4 * Math.pow(2.0f, (num-1) / NOTES_IN_OCTAVE)) / 16);
            NOTES[i] = new Note(i, pitch_octave[i], tempFreq);
            System.out.println(NOTES[i].getName());
            System.out.println(NOTES[i].getNumber());
            System.out.println(NOTES[i].getFrequency());
        }

    }

    public void createMaps() {

        String tempName;
        float tempFreq;
        int tempNum;

        for (int i = 0; i < NUM_TONES; ++i) {
            tempNum = NOTES[i].getNumber();
            tempName = NOTES[i].getName();
            tempFreq = NOTES[i].getFrequency();

            NumToFreq.put(tempNum, tempFreq);
            NumToName.put(tempNum, tempName);

            FreqToName.put(tempFreq, tempName);
            FreqToNum.put(tempFreq, tempNum);

            NameToFreq.put(tempName, tempFreq);
            NameToNum.put(tempName, tempNum);

            
        }

    }

}